//
//  FWPushController.m
//  BirdLoveSheep
//
//  Created by wwj on 16/3/24.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWPushController.h"
#import "FWVerticalBtn.h"
#import "NavigationController.h"
#import "FWPostWordController.h"
#import <POP.h>

static const CGFloat Speed = 10.0;

@interface FWPushController ()
@property (strong, nonatomic) NSArray *imgArray;
@property (strong, nonatomic) NSArray *titleArray;
@end

@implementation FWPushController

- (NSArray *)imgArray{
    if (_imgArray == nil) {
        _imgArray = @[@"publish-video", @"publish-picture", @"publish-text", @"publish-audio", @"publish-review", @"publish-offline"];
    }
    return _imgArray;
}

- (NSArray *)titleArray{
    if (_titleArray == nil) {
        _titleArray = @[@"发视频", @"发图片", @"发段子", @"发声音", @"审帖", @"离线下载"];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBgColor;
    
    CGFloat btnW = 72;
    CGFloat btnH = btnW + 30;
    CGFloat sBtnY = (kScreenH - 2 * btnH) * 0.5;
    CGFloat margin = 15;
    NSInteger maxCount = 3;
    CGFloat pading = (kScreenW - btnW * maxCount - 2 * margin) / (maxCount -1);
    for (NSInteger index = 0; index < self.imgArray.count; index ++) {
        FWVerticalBtn *btn = [[FWVerticalBtn alloc] init];
        btn.tag = index;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitle:self.titleArray[index] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:self.imgArray[index]] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        NSInteger col = index / maxCount;
        NSInteger row = index % maxCount;
        CGFloat btnY = sBtnY + btnH * col;
        CGFloat btnX = margin + (btnW + pading) * row;
        
        POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.beginTime = CACurrentMediaTime() + 0.05 * index;
        anim.fromValue = [NSValue valueWithCGRect:CGRectMake(btnX, -btnH, btnW, btnH)];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(btnX, btnY, btnW, btnH)];
        anim.springBounciness = Speed;
        anim.springSpeed = Speed;
        [btn pop_addAnimation:anim forKey:nil];
        
        [btn addTarget:self action:@selector(onClickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"app_slogan"]];
    imageView.center = CGPointMake(kScreenW * 0.5, -imageView.height);
    POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
    anim.toValue = [NSValue valueWithCGPoint:CGPointMake(kScreenW * 0.5, kScreenH * 0.2)];
    anim.beginTime = CACurrentMediaTime() + 0.05 * self.imgArray.count;
    anim.springBounciness = Speed;
    anim.springSpeed = Speed;
    [imageView pop_addAnimation:anim forKey:nil];
    [self.view addSubview:imageView];
}

- (void)onClickBtn:(FWVerticalBtn *)btn{
    [self dismessWithFinishBlock:^{
        if (btn.tag == 2) {
            UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
            FWPostWordController *postWordVC = [[FWPostWordController alloc] init];
            NavigationController *navigaVC = [[NavigationController alloc] initWithRootViewController:postWordVC];
            [rootVC presentViewController:navigaVC animated:YES completion:nil];
            
        }
    }];
    
}
- (IBAction)onClickCancel:(id)sender {
    [self dismessWithFinishBlock:nil];
}

- (void)dismessWithFinishBlock:(void (^)())finishBlock {
    for (int index = 2; index < self.view.subviews.count; index++) {
        UIView *view = self.view.subviews[index];
        POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame];
        anim.toValue = [NSValue valueWithCGRect:CGRectMake(view.x, kScreenH + view.height, view.width, view.height)];
        anim.beginTime = CACurrentMediaTime() + (index -2) * 0.1;
        if (index == self.view.subviews.count - 4) {
            [anim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                [self dismissViewControllerAnimated:NO completion:nil];
                if (finishBlock) {
                    finishBlock();
                }
            }];
        }
        [view pop_addAnimation:anim forKey:nil];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self dismessWithFinishBlock:nil];
}

@end
